package com.example.studentmanager.controller;

import com.example.studentmanager.model.Grade;
import com.example.studentmanager.model.vo.GradeVo;
import com.example.studentmanager.service.GradeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;
import java.util.List;



@Controller
@RequestMapping("grade")
public class GradeController {

    @Autowired
    GradeService gradeService;

    @Autowired
    Grade grade;

    @RequestMapping("listGrades")
    public String listGrades(Integer page,String searchCol,String searchValue,Model model){
        //1.获取用户查询的当前页
        page=page==null?1:page;
        //2.获取每页显示的条数
        Integer rows=2;

        //获取用户输入的条件
        System.out.println(searchCol+"----"+searchValue);
        //调用业务层
        //3.求每页显示的成绩列表
        List<GradeVo> grades = gradeService.listGrades(page,rows,searchCol,searchValue);
        //4.总记录数
        Integer counts =gradeService.counts(searchCol, searchValue);
        //5.总页数
        Integer pages=counts%rows==0?counts/rows:counts/rows+1;
        //2.设置在域中

        model.addAttribute("page", page);
        model.addAttribute("pages", pages);
        model.addAttribute("counts", counts);
        model.addAttribute("searchCol",searchCol);
        model.addAttribute("searchValue",searchValue);
        //3.转发到前端页面
        model.addAttribute("grades",gradeService.listGrades(page,rows,searchCol,searchValue));
        return "back/grade/grade";
    }


    @RequestMapping("writeScore")
    public String writeScore(String gradeid,String usual,String middle,String finalScore){
        grade.setGradeid(Integer.valueOf(gradeid));
        grade.setFinalexam(Integer.valueOf(finalScore));
        grade.setMiddleexam(Integer.valueOf(middle));
        grade.setUsualperformance(Integer.valueOf(usual));
        System.out.println(finalScore+"dewdwedwefew");
        gradeService.changeGrade(grade);
        return "back/grade/grade";
    }



    @RequestMapping("changeGrade")
    public String changeGrade(HttpSession session, Grade grade){
        gradeService.changeGrade(grade);
        return    "redirect:/grade/listGrades";
    }

}
